//
//  LoginViewController.m
//  TabulaImaginarium
//
//  Created by TA2 on 3/29/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "LoginViewController.h"

@implementation LoginViewController

@synthesize hostButton;
@synthesize connectButton;

- (void)dealloc {
    [dimmingLayerView release];
    dimmingLayerView = nil;
	[aliasField release];
    aliasField = nil;
	[localIpField release];
    localIpField = nil;
	[hostIpField release];
    hostIpField = nil;
	[hostButton release];
    hostButton = nil;
	[connectButton release];
    connectButton = nil;
	[workInProgressView release];
    workInProgressView = nil;
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
	aliasField.text = [UIDevice currentDevice].name;
	aliasField.placeholder = NSUserName();
	hostIpField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
	((Player *)[Players sharedLocalPlayer]).alias = aliasField.text;
	((Player *)[Players sharedLocalPlayer]).port = 50007;
	[aliasField setDelegate:self];
	[[NetworkController sharedNetworkController] startBonjourService];
    currentMapViewController = nil;

}

- (void)updateMethod {
	localIpField.text = [Players sharedLocalPlayer].ip;
}


- (IBAction)doHostButton:(id)sender {
	HostViewController *hostViewController = 
	[[HostViewController alloc] 
	 initWithNibName:@"HostViewController" bundle:nil];
	[self presentModalViewController:hostViewController animated:YES];
}

- (IBAction)doConnectButton:(id)sender {
	[self disableUI];
	dispatch_queue_t connectQueue = dispatch_queue_create("connectToHost", NULL);
	
	dispatch_async(connectQueue, ^{
		for (int waitingTimer = 0; waitingTimer < 1500000; waitingTimer += 1000) {
			usleep(1000);
			if ([NetworkController sharedNetworkController].hasFoundHost) {
				break;
			}
		}
		[self performSelectorOnMainThread:@selector(enableUI) withObject:nil waitUntilDone:NO];
	});
	[[NetworkController sharedNetworkController] joinSession:hostIpField.text port:((Player *)[Players sharedLocalPlayer]).port];
	
}

- (void)textFieldDidEndEditing: (UITextField *)textField {
	if (textField == aliasField) {
		((Player *)[Players sharedLocalPlayer]).alias = textField.text;
		[[NetworkController sharedNetworkController] startBonjourService];
	}
}

- (void)disableUI {
	[workInProgressView startAnimating];
	dimmingLayerView.alpha = 0.3;
	hostButton.enabled = NO;
	connectButton.enabled = NO;
	aliasField.enabled = NO;
	hostIpField.enabled = NO;
}

- (void)enableUI {
	[workInProgressView stopAnimating];
	dimmingLayerView.alpha = 0.0;
	hostButton.enabled = YES;
	connectButton.enabled = YES;
	aliasField.enabled = YES;
	hostIpField.enabled = YES;
	if ([NetworkController sharedNetworkController].hasFoundHost) {
		currentMapViewController = [[MapViewController alloc] initWithNibName:@"MapViewController" bundle:nil withMapName:nil];
		[[Players sharedPlayers] removeAllObjects];
		[self presentModalViewController:currentMapViewController animated:YES];
	}
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidAppear:(BOOL)animated {
	[[Players sharedPlayers] removeAllObjects];
	[[NetworkController sharedNetworkController] startBonjourBrowser];
    if (currentMapViewController)  {
        [currentMapViewController release];
        currentMapViewController = nil;
    }
    dispatch_queue_t updateQueue = dispatch_queue_create("loginUpdate", NULL);
	
	dispatch_async(updateQueue, ^{
		while ([NetworkController sharedNetworkController].bonjourIsActive) {
			[self performSelectorOnMainThread:@selector(updateMethod) withObject:nil waitUntilDone:NO];
			usleep(100000);
		}
	});	
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

@end
